/// @author: cairuoyu
/// @homepage: http://cairuoyu.com
/// @github: https://github.com/cairuoyu/cry、https://github.com/cairuoyu/flutter_admin
/// @date: 2021/6/21
/// @version: 1.0
/// @description:

import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_form_field.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:flutter_holo_date_picker/date_picker.dart';
import 'package:flutter_holo_date_picker/i18n/date_picker_i18n.dart';
import 'package:get/get_connect.dart';
import 'package:quiver/strings.dart';

class YearsDate extends CryFormField {
  YearsDate(
    BuildContext context, {
    Key? key,
    String? value,
    String? label,
    double? width,
    double? padding,
    ValueChanged? onChange,
    FormFieldValidator<String>? validator,
    FormFieldSetter? onSaved,
  }) : super(
          key: key,
          width: width,
          padding: padding,
          builder: (CryFormFieldState state) {
            return TextFormField(
              readOnly: true,
              decoration: InputDecoration(
                enabled: true,
                contentPadding: EdgeInsets.symmetric(horizontal: 10),
                labelText: label,
              ),
              controller: TextEditingController(text: value),
              onChanged: (v) {
                if (onChange != null) {
                  onChange(v);
                }
              },
              onTap: () async {
                // DateTime valueDt =
                //     isBlank(value) ? DateTime.now() : DateTime.parse(value!);
                // final DateTime? picked = await showDatePicker(
                //   context: context,
                //   initialDate: valueDt,
                //   firstDate: DateTime(1900, 1),
                //   lastDate: DateTime(2031, 12),
                // );
                // if (picked != null) {
                //   value = formatDate(picked, [yyyy, '-', mm, '-', dd]);
                // }
                // state.didChange();
                DateTime valueDt = DateTime.now();
                if(!isBlank(value)){
                  List<String> v = value!.split("-");
                  valueDt = DateTime(int.parse(v[0]),int.parse(v[1]));
                }
                var datePicked = await DatePicker.showSimpleDatePicker(
                  context,
                  initialDate: valueDt,
                  firstDate: DateTime(1960),
                  lastDate: DateTime(2044),
                  titleText: "选择日期",
                  confirmText: "确认",
                  cancelText: "取消",
                  dateFormat: "yyyy-MM",
                  locale: DateTimePickerLocale.zh_cn,
                  looping: true,
                );
                if (datePicked != null) {
                  value = formatDate(datePicked, [yyyy, '-', mm]);
                  state.didChange();
                }
                // state.didChange();
                // final snackBar =
                //     SnackBar(content: Text("Date Picked $datePicked"));
                // ScaffoldMessenger.of(context).showSnackBar(snackBar);
              },
              onSaved: onSaved,
              validator: (v) {
                if (validator != null) {
                  return validator(v);
                }
                return null;
              },
            );
          },
        );
}
